//
//  GLSLViewController.swift
//  ZSceneDemo
//
//  Created by 张鑫 on 2020/9/8.
//  Copyright © 2020 www.lionaitech.com. All rights reserved.
//

import UIKit

import SceneKit

extension GeometrySourceViewController {
    
    /**
     总结：
     1. 闭环的几何体，需要纹理正常铺满需要把闭环线上的点拆分
     */
    // MARK: - 四边形纹理效果实现
    func tetrahedron() {
        let vertex = [SCNVector3(0, 3, 3),
                      SCNVector3(0, 3, -3),
                      SCNVector3(3, -3, 0),
                      SCNVector3(-3, -3, 0),
                      SCNVector3(0, 3, 3),
                      SCNVector3(3, -3, 0),]
        
        let texture = [CGPoint(x: 0, y: 0),
                       CGPoint(x: 0.5, y: 0),
                       CGPoint(x: 0, y: 1),
                       CGPoint(x: 0.5, y: 1),
                       CGPoint(x: 1, y: 0),
                       CGPoint(x: 1, y: 1),]
        
        let normal = [SCNVector3(0, 0, 1), SCNVector3(0, 0, 1), SCNVector3(0, 0, 1), SCNVector3(0, 0, 1), SCNVector3(0, 0, 1), SCNVector3(0, 0, 1),]
        
        /// 创建接受顶点的对象
        let vertexSource = SCNGeometrySource(vertices: vertex)
        
        /// 创建纹理坐标对象
        let textureSource = SCNGeometrySource(textureCoordinates: texture)
        
        /// 法线坐标对象
        let normalSource = SCNGeometrySource(normals: normal)
        
        let indicesElement = SCNGeometryElement(indices: [0,2,1,3,4,5] as [Int32], primitiveType: .triangleStrip)
        
        let geometry = SCNGeometry(sources: [vertexSource, textureSource, normalSource], elements: [indicesElement])
        geometry.materials.first?.diffuse.contents = "art.scnassets/SunSysSource/earth-diffuse.jpg"
        
        sceneView?.scene?.rootNode.addChildNode(SCNNode(geometry: geometry))
        
        
        
    }
    
    
    
    
}
